/*
 ============================================================================
 Name		: FlashLightContainer.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CFlashLightContainer implementation
 ============================================================================
 */

#include <coemain.h>
#include "FlashLightContainer.h"
#include "Configuration.h"
#include "Utils.h"
#include "LMFileDefine.h"
#include "LMConfigDef.h"
#include "CommonUtils.h"
#include "MacroUtil.h"
#include "TimeOutTimer.h"
#include "LemonAssistant.hrh"
#include <LemonAssistant.mbg>
#include "LMSvgUtil.h"

#include "LMDebug.h"
#include "LAError.h"
#include "LemonAssistantAppUi.h"
#include "LemonAssistant.hrh"

const static int DISPLAY_TARGET = CHWRMLight::EPrimaryDisplayAndKeyboard;
const static int TURNOFF_DURATION = 1000;

CFlashLightContainer::CFlashLightContainer()
	{
	// No implementation required
	iLight = NULL;
	iTimer = NULL;
//	iVibraState = EVibraStateUnKnown;
	iLightState = EStrLightOff;
	}

CFlashLightContainer::~CFlashLightContainer()
	{
	SAFE_DELETE(iImgLight);
	SAFE_DELETE(iImgLightGray);
	SAFE_DELETE(iImgLightMask);
	
	SAFE_DELETE(iTimer);
	
	iLight->ReleaseLight(DISPLAY_TARGET);
	SAFE_DELETE(iLight);
	}

CFlashLightContainer* CFlashLightContainer::NewLC(const TRect& aRect)
	{
	CFlashLightContainer* self = new (ELeave) CFlashLightContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

CFlashLightContainer* CFlashLightContainer::NewL(const TRect& aRect)
	{
	CFlashLightContainer* self = CFlashLightContainer::NewLC(aRect);
	CleanupStack::Pop(); // self;
	return self;
	}

void CFlashLightContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
    
	SetRect(aRect);
	
	iLight = CHWRMLight::NewL(this);
	
	TInt target = iLight->SupportedTargets();
	if ((target & DISPLAY_TARGET) != DISPLAY_TARGET)
		{
		LAERRFUN(ELAWarnLightSurpport,ELMErrWarning)
		STATIC_CAST(CLemonAssistantAppUi*,CEikonEnv::Static()->AppUi())
					->ActivateLocalViewL(TUid::Uid(ELemonAssistantAppViewMain));
		return;
		}
	
	TRAPD(err,iLight->ReserveLightL(DISPLAY_TARGET))
	LAERR(err, ELAWarnLightReserve, ELMErrWarning)
	
	TFileName file;
	GetAppPath(file);
	file.Append(KConfigFile);
	CConfiguration* config = CConfiguration::NewL(file);
	TBuf<8> number;
	config->Get(KFlashLightColor,number);
	iColorIndex = CCommonUtils::StrToInt(number);
	
	number.Zero();
	config->Get(KLightOnDuration,number);
	iOnDuration = CCommonUtils::StrToInt(number);	
	
	number.Zero();
	config->Get(KLightOffDuration,number);
	iOffDuration = CCommonUtils::StrToInt(number);	
	
	//iColor = GetSettingColor(index);
	delete config;
	
	ResetColor();
	if (iColorIndex == ELightSettingColorTwinkle)
		{
		iTimer = CTimeOutTimer::NewL(CActive::EPriorityStandard,*this);
		TInt total = (iOnDuration+iOffDuration)*1000;
		iTimer->After(total);
		}
	
	iImgLight = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantFlashlight);
//	iImgLightGray = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantFlashlightgray);
	iImgLightMask = LMSvgUtil::GetImageFromResourceL(EMbmLemonassistantFlashlightmask);
	
	ActivateL();
	
	iLightState = EStrLightOn;
	LightOn();
	}

// ---------------------------------------------------------
// CFlashLightContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CFlashLightContainer::CountComponentControls() const
	{
	return 0; // return nbr of controls inside this container
	}

// ---------------------------------------------------------
// CFlashLightContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CFlashLightContainer::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		default:
			return NULL;
		}
	}

// ---------------------------------------------------------
// CFlashLightContainer::HandleControlEventL(
//	 CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CFlashLightContainer::HandleControlEventL(CCoeControl* /*aControl*/,
		TCoeEvent /*aEventType*/)
	{
	// TODO: Add your control event handler code here
	}

//------------------------------------------------------------------
//CFlashLightContainer::OfferKeyEventL(
// const TKeyEvent& aKeyEvent,	TEventCode aType)
//-------------------------------------------------------------------
//
TKeyResponse CFlashLightContainer::OfferKeyEventL(
		const TKeyEvent& /*aKeyEvent*/, TEventCode /*aType*/)
	{
	// See if we have a selection
//	switch (aType)
//		{
//		case EEventKey:
//			switch (aKeyEvent.iCode)
//				{
//				case EKeyDevice3://ok
//					Light();
//					return EKeyWasConsumed;
//				}
//			break;
//		}
	return EKeyWasNotConsumed;
	}
// -----------------------------------------------------------------------------
// CFlashLightContainer::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CFlashLightContainer::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();
	TRect drawRect(Rect());
	gc.Clear(drawRect);

	//add your code here...
	if (iLightState == EStrLightOn)
		{
		gc.SetBrushColor(iColor);
		gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.Clear(Rect());
		
		if (iImgLight)
			{
			TRect rect = iImgLight->SizeInPixels();
			TPoint point;
			point.iX = (Rect().Width() - rect.Width()) >> 1;
			point.iY = (Rect().Height() - rect.Height()) >> 1;
			gc.BitBltMasked(point,iImgLight,rect,iImgLightMask,ETrue);
			//gc.BitBlt(point,iImgLightMask);
			}
		}
	else
		{
		if (iImgLightGray)
			{
			TRect rect = iImgLightGray->SizeInPixels();
			TPoint point;
			point.iX = (Rect().Width() - iImgLightGray->SizeInPixels().iWidth) >> 1;
			point.iY = (Rect().Height() - iImgLightGray->SizeInPixels().iHeight) >> 1;
			gc.BitBltMasked(point,iImgLightGray,rect,iImgLightMask,ETrue);
			//gc.BitBlt(point,iImgLightGray);
			}
		}
	}

// -----------------------------------------------------------------------------
// CFlashLightContainer::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CFlashLightContainer::SizeChanged()
	{
	DrawNow();
	}

void CFlashLightContainer::LightStatusChanged(TInt aTarget, 
                        CHWRMLight::TLightStatus aStatus)
    {        
//	_LIT8(KChangeInfo,"LightStatusChanged target:%d,status:%d");
//	TBuf8<50> info;
//	info.Format(KChangeInfo,aTarget,aStatus);
//	__LOGDES_TOFILE(info);
	
      if(!aTarget)
        {
        //No target specified
        }
    
        switch ( aStatus )
            {
            case CHWRMLight::ELightOn:
                {
              //ELightOn do something... 
              break;
                }
            case CHWRMLight::ELightOff:
                {
//    			if(iVibraState != EVibraStateUnKnown)
//    			if (iLightState == EStrLightOn)
//    				{
//    				LightOn();
//    				}
                //ELightOff do something... 
                break;
                }
            case CHWRMLight::ELightBlink:
                {
//                iVibraState = EVibraStateRunning;
                //ELightBlink do something... 
                break;
                }
            case CHWRMLight::ELightStatusUnknown:
                {
                //ELightStatusUnknown do something... 
                break;
                }
            default:
                {
                //Lights status undefined do something... 
                break;
                }
            }
 
    }

void CFlashLightContainer::Light()
	{
	if (iLightState == EStrLightOn)
		{
		LightOff();
		iLightState = EStrLightOff;
		}
	else if (iLightState == EStrLightOff)
		{
		LightOn();
		iLightState = EStrLightOn;
		}
//	iVibraState = EVibraStateUnKnown;
	DrawNow();
	}

void CFlashLightContainer::LightOn()
	{
	TRAPD(err,iLight->LightBlinkL(DISPLAY_TARGET,KHWRMInfiniteDuration,
			iOnDuration,iOffDuration,KHWRMDefaultIntensity))
	
	LAERR(err, ELAWarnLighBlink, ELMErrWarning)
	
	if (err == KErrNone)
		{
		
		}
	else
		{
		
		}
	
	}

void CFlashLightContainer::LightOff()
	{
	TRAPD(err,iLight->LightOffL(DISPLAY_TARGET,KHWRMLightMaxDuration))

	TRAP(err,iLight->LightOnL(DISPLAY_TARGET))
	
	LAERR(err, ELAWarnLightOff, ELMErrWarning)
	
	if (err == KErrNone)
		{		
		} 
	else
		{		
		}
	
	}

void CFlashLightContainer::TimerExpired()
	{
	ResetColor();
	DrawNow();
	
	TInt total = (iOnDuration+iOffDuration)*1000;
	iTimer->After(total);
	}

void CFlashLightContainer::ResetColor()
	{
	if (iColorIndex == ELightSettingColorTwinkle)
		iColor = GetSettingColor(ELightSettingColorRandom);
	else
		iColor = GetSettingColor(iColorIndex);
	}

void CFlashLightContainer::HandlePointerEventL(const TPointerEvent& aEvent)
	{
	/*
	//	if ( AknLayoutUtils::PenEnabled() )
	if (OKCUI()->CheckPenEnabled())
		{
		CCoeControl::HandlePointerEventL(aEvent);
		
		if (aEvent.iType == 1 && CCommonUtils::IsPointInRect(
				aEvent.iPosition, Rect()))
			{
			Light();
			DrawNow();
			}
		}
		*/
	}

void CFlashLightContainer::HandleResourceChange( TInt aType )
    {
    CCoeControl::HandleResourceChange( aType );
    if  ( aType == KEikDynamicLayoutVariantSwitch )
    	{
        TRect rect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
        SetRect(rect);
        }
    }
// End of File
